package com.company.huisuo.biz.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.company.huisuo.R
import com.company.huisuo.base.BaseSingleAdapter
import com.company.huisuo.biz.bean.AppGameParticipantRespVO
import com.company.huisuo.databinding.ItemGameGroupBinding



class GameGroupAdapter : BaseSingleAdapter<AppGameParticipantRespVO, ItemGameGroupBinding>() {

    var mSelectPos = -1
    var onDeleteClick: ((AppGameParticipantRespVO, Int) -> Unit)? = null

    override val initViewBinding: (LayoutInflater, ViewGroup, Boolean) -> ItemGameGroupBinding
        get() = ItemGameGroupBinding::inflate

    override fun convert(
        holder: SingleViewHolder<ItemGameGroupBinding>, position: Int, item: AppGameParticipantRespVO?
    ) {
        holder.binding.apply {
            tvTitle.text = "组合0${position + 1}"
            // 根据位置判断是否为中心Item，设置对应背景
            val backgroundRes = if (mSelectPos == position) {
                R.drawable.ic_game_group_center_bg
            } else {
                R.drawable.bg_game_group_other
            }
            clLayout.setBackgroundResource(backgroundRes)
            
            // 设置参与人信息
            item?.let { participant ->
                // 设置客户名称（显示在女性角色下方）
                tvFemaleName.text = participant.customerName
                
                // 设置艺人名称（显示在男性角色下方）
                tvMaleName.text = participant.artistName
            }
            
            // 设置关闭按钮点击事件
            ivClose.setOnClickListener {
                item?.let { participant ->
                    onDeleteClick?.invoke(participant, position)
                }
            }
        }
    }

    fun selectPos(pos: Int) {
        mSelectPos = pos
        notifyDataSetChanged()
    }
    
    /**
     * 获取当前选中的参与人
     */
    fun getSelectedParticipant(): AppGameParticipantRespVO? {
        return if (mSelectPos >= 0 && mSelectPos < itemCount) {
            getItem(mSelectPos)
        } else {
            null
        }
    }
}